unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, Menus,
  addComment, allComments;

type
  TForm1 = class(TForm)
    lbSourceCode: TListBox;
    bLoadSourceCode: TButton;
    PopupMenu1: TPopupMenu;
    miAddComment: TMenuItem;
    miViewComment: TMenuItem;
    miViewAllComments: TMenuItem;
    lbNbOfComments: TListBox;
    miDeleteComment: TMenuItem;
    Label1: TLabel;
    Label2: TLabel;
    procedure bLoadSourceCodeClick(Sender: TObject);
    procedure lbSourceCodeClick(Sender: TObject);
    procedure PopupMenu1Popup(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure miAddCommentClick(Sender: TObject);
    procedure miDeleteCommentClick(Sender: TObject);
    procedure miViewAllCommentsClick(Sender: TObject);
  private
    { Private declarations }
    mSelectedLine : Integer;
    mTotalNbOfComments: Integer;
    mNbOfLines: Integer;
    mNbOfCommentsArray : array of Integer;
    mAllCommentStrings : array of TStringList;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
  mSelectedLine := -1; //no line is selected
  mTotalNbOfComments := 0;
  mNbOfLines := 0;
end;

procedure TForm1.bLoadSourceCodeClick(Sender: TObject);
var
  codeStrings : TStringList;
  i : Integer;
begin
  mSelectedLine := -1; //no line is selected
  mTotalNbOfComments := 0;

  //read souce code lines:
  codeStrings := TStringList.Create;
  codeStrings.LoadFromFile('test.txt');
  mNbOfLines := codeStrings.Count;

  //clear list boxes:
  lbNbOfComments.Clear;
  lbSourceCode.Clear;

  //set dynamic array sizes:
  SetLength(mNbOfCommentsArray, mNbOfLines-1);
  SetLength(mAllCommentStrings, mNbOfLines-1);

  //add strings with line numbers:
  for i:= 0 to mNbOfLines-1 do
  begin
    mNbOfCommentsArray[i] := 0;
    lbNbOfComments.Items.Add(IntToStr(mNbOfCommentsArray[i]));
    lbSourceCode.Items.Add(IntToStr(i+1) + '. ' + codeStrings[i]);
  end;
end;

procedure TForm1.lbSourceCodeClick(Sender: TObject);
var
  i: Integer;
begin
  for i := 0 to (lbSourceCode.Items.Count - 1) do
  begin
    if lbSourceCode.Selected[i] then
    begin
      mSelectedLine := i;
      PopupMenu1.AutoPopup := True;
      Break;
    end;
  end;
end;

procedure TForm1.PopupMenu1Popup(Sender: TObject);
begin
  if mSelectedLine > -1 then //if a line is selected
  begin
    miAddComment.Caption  := '&Add comment to line '+IntToStr(mSelectedLine+1);
    miViewComment.Caption := '&View comment on line '+IntToStr(mSelectedLine+1);
    if mNbOfCommentsArray[mSelectedLine] > 0 then
    begin
      miViewComment.Enabled := True;
      miDeleteComment.Enabled := True;
      miDeleteComment.Caption := '&Delete comment on line '+IntToStr(mSelectedLine+1);
    end
    else
    begin
      miViewComment.Enabled := False;
      miDeleteComment.Enabled := False;
    end;
    if mTotalNbOfComments > 0 then
    begin
      miViewAllComments.Enabled := True;
    end
    else
    begin
      miViewAllComments.Enabled := False;
    end;
  end;
end;

procedure TForm1.miAddCommentClick(Sender: TObject);
var
  commentStrings: TStringList;
begin
  commentStrings := formAddComment.getComment;

  mAllCommentStrings[mSelectedLine] := TStringList.Create;
  mAllCommentStrings[mSelectedLine].AddStrings(commentStrings);

  mNbOfCommentsArray[mSelectedLine] := mNbOfCommentsArray[mSelectedLine]+1;
  mTotalNbOfComments := mTotalNbOfComments+1;
  lbNbOfComments.Items[mSelectedLine] := IntToStr(mNbOfCommentsArray[mSelectedLine]);
end;

procedure TForm1.miDeleteCommentClick(Sender: TObject);
begin
  mNbOfCommentsArray[mSelectedLine] := mNbOfCommentsArray[mSelectedLine]-1;
  mTotalNbOfComments := mTotalNbOfComments-1;
  lbNbOfComments.Items[mSelectedLine] := IntToStr(mNbOfCommentsArray[mSelectedLine]);
end;

procedure TForm1.miViewAllCommentsClick(Sender: TObject);
begin
  formAllComments.showAllComments(mNbOfLines, mAllCommentStrings);
end;

end.
